package com.itic.mall.mobile.goods.sku.coupon;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.utils.NumberStringHelper;
import com.itic.appbase.framework.utils.NumericHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.system.goods.goodscategory.persistence.model.GoodsCategory;
import com.itic.mall.system.goods.skucoupon.persistence.model.SkuCoupon;
import com.itic.mall.system.goods.skucoupon.persistence.model.SkuCouponExample;
import com.itic.mall.system.goods.skucoupon.persistence.model.SkuCouponExample.Criteria;
import com.itic.mall.system.goods.skucoupon.service.SkuCouponService;

/**
 * 商品SKU的优惠券信息
 * @author 王文鑫
 * @date 2018年1月10日 下午3:39:51
 */
@Controller
@RequestMapping(value = "/mobile/goods/sku/coupon")
public class SkuCouponInterface extends BaseMobileInterface {
	
	@Autowired
	private SkuCouponService skuCouponService;
	
	/**
	 * 根据skuid获取商品优惠信息
	 * @author 王文鑫
	 * @date 2018年1月10日 下午3:33:02
	 * @param request
	 * @param response
	 * @param id
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/getSkuCouponBySkuId")
	@ResponseBody
	public Map<String, Object> getSkuCouponBySkuId(HttpServletRequest request, HttpServletResponse response, String skuId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		SkuCouponExample example = new SkuCouponExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);// 是否有效
		List<SkuCoupon> list = new ArrayList<>();
		
		//判断参数为空
		if(StringHelper.isNotBlank(skuId)){
			criteria.andSkuIdEqualTo(skuId);
			list = skuCouponService.list(example);// 数据list
		}
		
		resultMap.put("list", ValueHelper.tranList2MapList(list,
				new String[] { "totalFeedback", "isServenBack", "detailImageUrls", "reorder", "note", "updateTime", "createTime", "orgCode","createUser"}));
		return resultMap;
	}
	
	
	/**
	 * 商品详情页领取券   券数量减少
	 * @author 王文鑫
	 * @date 2018年1月10日 下午4:22:45
	 * @param request
	 * @param response
	 * @param skuId
	 * @return
	 */
	@RequestMapping(value = "/getCouponNumById")
	@ResponseBody
	public Map<String, Object> getCouponNumById(HttpServletRequest request, HttpServletResponse response, String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String result = ResponseCode.SERVER_ERROR.getCode();
		SkuCoupon skuCoupon = new SkuCoupon();
		
		//判断参数为空
		if(StringHelper.isNotBlank(id)){
			skuCoupon = skuCouponService.get(id);//更具卷id获取卷的信息
			if(null != skuCoupon ){
				int skunum = skuCoupon.getTotalCount();//优惠券总数量（-1为无限制）
				if(skunum == -1){
					result = ResponseCode.SUCCESS.getCode();//返回成功
				}else if(skunum > 0 ){
					int skunum1 = NumericHelper.subInteger(skunum, 1);//优惠券数量-1
					skuCoupon.setTotalCount(skunum1);//设置优惠券数量参数
					result = skuCouponService.edit(skuCoupon);//修改卷的信息
				}else{
					result = ORDER_COUPON_ERROR_MSG;//优惠券信息为空
				}
			}else{
				result = OBJECT_ERROR_MSG;//获取对象错误
			}
		}else{
			result = PARAM_ERROR_MSG;//缺少参数
		}
		
		resultMap.put(IticConst.RESULT, result);
		return resultMap;
	}

}
